package WIAB::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';

__PACKAGE__->config->{namespace} = '';


=head2 METHOD

index : Private

=head3 DESCRIPTION

Redirect to content page. Could be used to provide a splash page as
well.

=cut

sub index : Private {
    my ($self, $c) = @_;
    $c->res->redirect($c->uri_for('/content'));
    $c->detach();
}

=head2 METHOD

login : Local

=head3 DESCRIPTION

Redirects the user to the user controller.  This is here to give the
user an easy to remember url but to keep the user controller
gramatical.

=cut

sub login : Local  {
    my ($self, $c) = @_;
    $c->detach('user/login');
}

=head2 METHOD

default : Private

=head3

Global 404 handler

=cut

sub default : Private {
    my ($self, $c) = @_;
    $c->res->status(404);
    $c->res->body('404 not found');
}


=head2 METHOD

end : ActionClass('RenderView')

=head3 DESCRIPTION

Renders the view. - sets a couple of custom headers including a sane
last-modified for L<Content> pages.

=cut

sub end : ActionClass('RenderView') {
    my ( $self, $c ) = @_;

    # Forward to View unless response body is already defined
    $c->forward( $c->view('TT') )
        unless $c->response->body
            || $c->response->status =~ /^3\d\d$/;

    # Set our headers
    if ($c->user) {
        # don't cache stuff from logged in users
        $c->response->headers->header(
            'Last-Modified' => localtime(),
            'Cache-Control' => 'max-age=0, no-cache',
        );
    }
    else {
        $c->response->headers->header(
            'Last-Modified' => $c->stash->{last_modified},
            'Cache-Control' => 'max-age=3600, must-revalidate',
        );

    }
}


=head1 NAME

WIAB::Controller::Root - Root Controller for this Catalyst based application

=head1 SYNOPSIS

See L<WIAB>.

=head1 DESCRIPTION

Root Controller for this Catalyst based application.

=head1 METHODS

=cut

=head1 AUTHOR

Kieren Diment

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
